package com.yuejian_06.yuejian_06.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.yuejian_06.yuejian_06.R;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements View.OnClickListener{

    private LinearLayout TabActivity;
    private LinearLayout TabCoach;
    private LinearLayout TabContest;

    private ImageView ImgActivity;
    private ImageView ImgCoach;
    private ImageView ImgContest;

    private Fragment fragment_activity;
    private Fragment fragment_coach;
    private Fragment fragment_contest;


    private RollPagerView mRollViewPager;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_home, container, false);
        mRollViewPager = (RollPagerView) view.findViewById(R.id.roll_view_pager);
        TabActivity = (LinearLayout) view.findViewById(R.id.id_tab_activity);
        TabCoach = (LinearLayout) view.findViewById(R.id.id_tab_coach);
        TabContest = (LinearLayout) view.findViewById(R.id.id_tab_contest);

        ImgActivity = (ImageView) view.findViewById(R.id.id_tab_activity_img);
        ImgCoach = (ImageView) view.findViewById(R.id.id_tab_coach_img);
        ImgContest = (ImageView) view.findViewById(R.id.id_tab_contest_img);
        initEvent();//监听
        setSelect(0);//选择了一个默认的fragment

        //设置播放时间间隔
        mRollViewPager.setPlayDelay(2000);
        //设置透明度
        mRollViewPager.setAnimationDurtion(500);
        //设置适配器
        mRollViewPager.setAdapter(new TestNormalAdapter());

        //设置指示器（顺序依次）
        //自定义指示器图片
        //设置圆点指示器颜色
        //设置文字指示器
        //隐藏指示器
        //mRollViewPager.setHintView(new IconHintView(this, R.drawable.point_focus, R.drawable.point_normal));
        mRollViewPager.setHintView(new ColorPointHintView(getActivity(),Color.YELLOW, Color.WHITE));
        //mRollViewPager.setHintView(new TextHintView(this));
        //mRollViewPager.setHintView(null);

        return view;
    }

    private void initEvent(){
        TabActivity.setOnClickListener(this);
        TabCoach.setOnClickListener(this);
        TabContest.setOnClickListener(this);
    }

    private void resetImgs()
    {
        ImgActivity.setImageResource(R.drawable.tab_activity_n);
        ImgCoach.setImageResource(R.drawable.tab_coach_n);
        ImgContest.setImageResource(R.drawable.tab_contest_n);
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i)
        {
            case 0:
                if (fragment_activity == null)
                {
                    fragment_activity = new ActivityFragment();
                    transaction.add(R.id.id_home_content, fragment_activity);
                } else
                {
                    transaction.show(fragment_activity);
                }
                ImgActivity.setImageResource(R.drawable.tab_activity_p);
                break;

            case 1:
                if (fragment_coach == null)
                {
                    fragment_coach = new CoachFragment();
                    transaction.add(R.id.id_home_content, fragment_coach);
                } else
                {
                    transaction.show(fragment_coach);
                }
                ImgCoach.setImageResource(R.drawable.tab_coach_p);
                break;


            case 2:
                if (fragment_contest == null)
                {
                    fragment_contest = new ContestFragment();
                    transaction.add(R.id.id_home_content, fragment_contest);
                } else
                {
                    transaction.show(fragment_contest);

                }
                ImgContest.setImageResource(R.drawable.tab_contest_p);
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (fragment_activity != null)
        {
            transaction.hide(fragment_activity);
        }
        if (fragment_coach != null)
        {
            transaction.hide(fragment_coach);
        }
        if (fragment_contest != null)
        {
            transaction.hide(fragment_contest);
        }
    }

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_activity:
                setSelect(0);
                break;
            case R.id.id_tab_coach:
                setSelect(1);
                break;
            case R.id.id_tab_contest:
                setSelect(2);
                break;

            default:
                break;
        }
    }



    private class TestNormalAdapter extends StaticPagerAdapter {
        private int[] imgs = {
                R.drawable.lb1,
                R.drawable.lb2,
                R.drawable.lb3,
                R.drawable.lb4,
        };

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            view.setImageResource(imgs[position]);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }

        @Override
        public int getCount() {
            return imgs.length;
        }
    }




}
